// pages/repair/repair.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    array: ['取暖器', '扣板', '照明', '墙面', '其他'],
    images: [],
    userinfo: "",
    typename: "请选择维修类型",
    imagesno: "",
    remark: ""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

      var _this = this
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求    
            wx.setStorageSync('code', res.code)
            _this.getopenid(res.code)
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      })
    


  },
  getopenid: function(code) {
    var openid = wx.getStorageSync('openid')
    if (openid != '') {
      this.setData({
        openid: openid
      })
      return;
    }

    var url = "https://www.d-shang.com/static/wxapp/getsession.php?code=" + code
    var _this = this;
    wx.request({
      url: url,
      success: function(res) {
        var openid = res.data.openid
        console.log(res)
        wx.setStorageSync('openid', res.data.openid)
        wx.setStorageSync('session_key', res.data.openid)
      }
    })
  },
  getPhoneNumber: function(e) {
    var _this = this
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function(res) {}
      })
    } else {
      _this.getUserMobile(e.detail)
    }
  },
  gotoShow: function() {
    //每次都是新的
    this.getImageSNO()

    var _this = this
    wx.chooseImage({
      count: 9, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res) {
        // success

        _this.setData({
          images: res.tempFilePaths
        })

        for (var i = 0; i < res.tempFilePaths.length; i++) {
          _this.uploadFile(res.tempFilePaths[0])
        }


      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete

      }
    })
  },
  getUserMobile: function(data) {
    console.log(data)
    var code = wx.getStorageSync('code')
    wx.request({
      url: 'https://www.d-shang.com/static/wxapp/wx.php', //腾讯官方demo改造的接口页面
      data: {
        'encryptedData': data.encryptedData, //需要进行编码
        'iv': data.iv,
        'session_key': code
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      }, // 设置请求的 header
      success: function(data2) {
        console.log(data2)
      },
      fail: function(err) {
        console.log(err);
      }
    })
  },
  subform: function() {
    var url = "https://www.d-shang.com/index.php?fixed/addform/"
    var formdata = {
      username: this.data.userinfo.username,
      mobile: this.data.userinfo.telNumber,
      address: this.data.userinfo.address,
      typename: this.typename,
      remark: this.data.remark,
      imageSNO: this.data.imagesno
    }
    var label={
      username:"客户姓名",
      mobile:"手机号码",
      address:"收货地址",
      typename:"维修项目",
      remark:"备注说明"
    }
    var data={data:formdata,label:label}
    var _this=this
    wx.request({
      url: url,
      method: 'POST',
      data: {
        title: "维修工单",
        openid: this.data.openid,
        content: data
      },
      success: function(res) {
         _this.jumpPage()
      }
    })
  },
  uploadFile: function(filepath) {
    wx.uploadFile({
      url: 'https://www.d-shang.com/index.php?appimage/upload/?openid=' + this.data.openid + '&sno=' + this.data.imagesno,
      filePath: filepath,
      name: 'file',
      formData: {
        user: 'test'
      },
      success(res) {
        console.log(res.data)
      }
    })
  },
  getImageSNO: function() {
    var url = 'https://www.d-shang.com/index.php?appimage/getimagesno/?openid=' + this.data.openid
    var _this = this
    wx.request({
      url: url,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        _this.setData({
          imagesno: res.data.data
        })
      }
    })
  },
  jumpPage: function() {
    wx.showModal({
      title: '提示',
      content: '提交成功，等待审核',
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/worksheet/worksheet'
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  bindPickerChange: function(res) {
    this.setData({
      typename: this.data.array[res.detail.value]
    })
  },
  getUserInfo: function() {
    var _this = this;
    wx.chooseAddress({
      success(res) {
        var info = {
          "username": res.userName,
          "postalCode": res.postalCode,
          "address": res.provinceName + res.countyName + res.countyName + res.detailInfo,
          "telNumber": res.telNumber,
          "nationalCode": res.nationalCode
        }
        _this.setData({
          userinfo: info
        })

      }
    })

  }
})